package com.fs.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.example.studentmanagersystem.AdminMainActivity;
import com.example.studentmanagersystem.MainActivity;
import com.example.studentmanagersystem.StudentMainActivity;
import com.example.studentmanagersystem.TeacherMainActivity;
import com.fs.po.UserLogin;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class LoginHttpUtil {
	
	public static final String SUCCESS_RESULT_CODE="200";
	public static final String FAIL_RESULT_CODE="400";
	private Context context;
	
	private Handler mHandler;
	
	public LoginHttpUtil(Context context,Handler mHandler) {

		this.context=context;
		this.mHandler=mHandler;
	}

	/**
	 * 主界面登录数据提交到服务器
	 */
	int isSuccess=0;
	public void mainActivityLoginSubmit(String action,String usernameID,String userpasswordID,final UserLogin user,String url,final int type) {
		System.out.println(usernameID+":"+userpasswordID+":"+user.toString());//【测试】

		//提交的数据
		RequestParams params = new RequestParams();
		params.addBodyParameter(usernameID, user.getUsername());
		params.addBodyParameter(userpasswordID, user.getUserpassword());
		params.addBodyParameter("action", action);
		HttpUtils http = new HttpUtils();
		System.out.println("发送请求前");
		//发送请求：Post(请求方式)，url(访问地址)，params(数据)，请求事件监听；
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				
//				Toast.makeText(context, "Stu返回结果"+responseInfo.result, 0).show();//【测试】
				
				if(type==0){
					
					if(SUCCESS_RESULT_CODE.equals(responseInfo.result)){//"200"——responseInfo.result
						Intent intent=new Intent(context,AdminMainActivity.class);
						context.startActivity(intent);
						System.out.println("管理员成功码返回："+responseInfo.result);//【测试】
						Toast.makeText(context, "管理员登录成功", 0).show();
						
						isSuccess=1;
						
					}else {
						System.out.println("管理员失败码返回："+responseInfo.result);//【测试】
						Toast.makeText(context, "登录失败,用户名或密码错误", 0).show();
					}
				}else if(type==1){
					
					if(responseInfo.result!=""){
						
						MainActivity._id=Integer.parseInt(responseInfo.result);
						Intent intent=new Intent(context,StudentMainActivity.class);
						context.startActivity(intent);
						
						isSuccess=1;
						
					}else {
						System.out.println("学生员失败码返回："+responseInfo.result);//【测试】
						Toast.makeText(context, "登录失败,用户名或密码错误", 0).show();
					}
					
				}else if(type==2){
					
					if(responseInfo.result!=""){
						
						MainActivity._id=Integer.parseInt(responseInfo.result);
						Intent intent=new Intent(context,TeacherMainActivity.class);
						context.startActivity(intent);
						 
						Toast.makeText(context, "老师登录成功！", 0).show();
						
						isSuccess=1;
						
					}else {
						
						Toast.makeText(context, "登录失败,用户名或密码错误", 0).show();
					}
				}
				
				
				new Thread(){
					@Override
					public void run() {
						super.run();
						
						Message msg=Message.obtain();
						
						msg.arg1=isSuccess;
						mHandler.sendMessage(msg);
					}
					
				}.start();
			}
			
			@Override
			public void onFailure(HttpException error, String msg) {
 
				if(type==0){
					
				}else if(type==1){
					
				}else if(type==2){
					
				}
			}

		});
	}

}
